package dp.test213;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/23 12:03
 */
public class Solution1 {

    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) return 0;
        int n = nums.length;
        if (n == 1) return nums[0];
        return Math.max(helper(nums, 0, n - 2), helper(nums, 1, n - 1));
    }

    private int helper(int[] nums, int l, int r) {
        if (l == r) return nums[l];
        int c1 = 0, c2 = nums[l], ans = nums[l];
        for (int i = l + 1; i <= r; i++) {
            ans = Math.max(c2, c1 + nums[i]);
            c1 = c2;
            c2 = ans;
        }
        return ans;
    }
}
